package com.andnux.android.dialog;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;

import java.util.ArrayList;
import java.util.List;

public class DialogFactory {

    public interface OnSingleClickListener {
        /**
         * This method will be invoked when a button in the dialog is clicked.
         *
         * @param dialog the dialog that received the click
         * @param which  the button that was clicked (ex.
         *               {@link DialogInterface#BUTTON_POSITIVE}) or the position
         *               of the item clicked
         */
        void onClick(DialogInterface dialog, int which);
    }

    public interface OnEditInputClickListener {
        /**
         * This method will be invoked when a button in the dialog is clicked.
         *
         * @param dialog  the dialog that received the click
         * @param content the button that was clicked (ex.
         *                {@link DialogInterface#BUTTON_POSITIVE}) or the position
         *                of the item clicked
         */
        void onClick(DialogInterface dialog, String content);
    }

    public interface OnwMultiClickListener {
        /**
         * This method will be invoked when a button in the dialog is clicked.
         *
         * @param dialog the dialog that received the click
         * @param which  the button that was clicked (ex.
         *               {@link DialogInterface#BUTTON_POSITIVE}) or the position
         *               of the item clicked
         */
        void onClick(DialogInterface dialog, List<Integer> which);
    }


    /**
     * @param context
     * @param title
     * @param content
     * @param listener
     */
    public static void showNormalDialog(Context context, String title, String content, OnSingleClickListener listener) {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context);
        normalDialog.setTitle(title);
        normalDialog.setMessage(content);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (listener != null) {
                            listener.onClick(dialog, which);
                        }
                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        // 显示
        normalDialog.show();
    }

    /**
     * @param context
     * @param title
     * @param items
     * @param listener
     */
    public static void showListDialog(Context context, String title, String[] items,
                                      OnSingleClickListener listener) {
        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(context);
        listDialog.setTitle(title);
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // which 下标从0开始
                if (listener != null) {
                    listener.onClick(dialog, which);
                }
            }
        });
        listDialog.show();
    }

    /**
     * @param context
     * @param title
     * @param items
     * @param listener
     */

    public static void showSingleChoiceDialog(Context context, String title, String[] items, int checkedItem,
                                              OnSingleClickListener listener) {
        yourChoices.clear();
        yourChoices.add(checkedItem);
        AlertDialog.Builder singleChoiceDialog =
                new AlertDialog.Builder(context);
        singleChoiceDialog.setTitle(title);
        // 第二个参数是默认选项，此处设置为0
        singleChoiceDialog.setSingleChoiceItems(items, checkedItem,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        yourChoices.clear();
                        yourChoices.add(which);
                    }
                });
        singleChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (yourChoices.size() != 0) {
                            if (listener != null) {
                                listener.onClick(dialog, yourChoices.get(0));
                            }
                        }
                    }
                });
        singleChoiceDialog.show();
    }

    /**
     * @param context
     * @param title
     * @param items
     * @param listener
     */
    private static ArrayList<Integer> yourChoices = new ArrayList<>();

    public static void showMultiChoiceDialog(Context context, String title, String[] items,
                                             OnwMultiClickListener listener) {
        // 设置默认选中的选项，全为false默认均未选中
        final boolean[] initChoiceSets = new boolean[items.length];
        yourChoices.clear();
        AlertDialog.Builder multiChoiceDialog =
                new AlertDialog.Builder(context);
        multiChoiceDialog.setTitle(title);
        multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                                        boolean isChecked) {
                        if (isChecked) {
                            yourChoices.add(which);
                        } else {
                            yourChoices.remove(which);
                        }
                    }
                });
        multiChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (yourChoices.size() != 0) {
                            if (listener != null) {
                                listener.onClick(dialog, yourChoices);
                            }
                        }
                    }
                });
        multiChoiceDialog.show();
    }

    /**
     * @param context
     * @param title
     * @param content
     * @return
     */
    public static ProgressDialog showWaitingDialog(Context context, String title, String content) {

        ProgressDialog waitingDialog =
                new ProgressDialog(context);
        waitingDialog.setTitle(title);
        waitingDialog.setMessage(content);
        waitingDialog.setIndeterminate(true);
        waitingDialog.setCancelable(false);
        waitingDialog.show();
        return waitingDialog;
    }

    /**
     * @param context
     * @param title
     * @param content
     * @param hint
     * @param listener
     */
    public static void showInputDialog(Context context, String title, String content, String hint, OnEditInputClickListener listener) {
        final EditText editText = new EditText(context);
        editText.setText(content);
        editText.setHint(hint);
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(context);
        inputDialog.setTitle(title).setView(editText);
        inputDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (listener != null) {
                    listener.onClick(dialog, editText.getText().toString());
                }
            }
        }).show();
    }

}
